package com.example.demo2.controller;

import com.example.demo2.mapper.UserMapper;
import org.springframework.core.io.Resource;

import org.apache.ibatis.type.NStringTypeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/register")
public class registerController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/register")
    public String register(@RequestParam("uName") String uName,
                           @RequestParam("uPw") String uPw,
                           @RequestParam("uEmail") String uEmail,
                           @RequestParam("uNum") String uNum,
                           @RequestParam("uCompany") String uCompany,
                           @RequestParam("uRealName") String uRealName,
                           @RequestParam("uPic") MultipartFile uPic) {
        try {

            if (userMapper.checkUserExists(uName) > 0) {
                return "用户名已存在，请选择其他用户名";
            }


            String fileName = System.currentTimeMillis() + "_" + uPic.getOriginalFilename();
            String filePath = "E://registerphoto/" + fileName;
            File dest = new File(filePath);
            uPic.transferTo(dest);


            String uPicT  = "http://localhost:8080/files/images/" + filePath;
            userMapper.register(uName, uPw, uEmail, uNum, uCompany, uRealName, uPicT);

            return "用户注册成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "用户注册失败: " + e.getMessage();
        }
    }
    @GetMapping("/getUserPic")
    public ResponseEntity<Resource> getUserPic(@RequestParam("uName") String uName) {
        try {
            System.out.println("[INFO] 正在处理获取用户图片的请求，用户名：" + uName);

            // 从数据库获取图片路径
            String picPath = userMapper.getUserPicPath(uName);
            if (picPath == null) {
                System.out.println("[WARN] 数据库中未找到对应的图片路径，用户名：" + uName);
                return ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(null); // 404 未找到
            }
            System.out.println("[INFO] 成功从数据库获取图片路径：" + picPath);

            // 检查图片文件是否存在
            File file = new File(picPath);
            if (!file.exists()) {
                System.out.println("[ERROR] 文件路径不存在，路径：" + picPath);
                return ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(null); // 404 未找到
            }

            // 构建资源对象
            Resource resource = new UrlResource(file.toURI());
            System.out.println("[INFO] 文件存在且资源已成功加载，准备返回图片数据");

            // 返回图片文件作为响应
            return ResponseEntity.ok()
                    .contentType(MediaType.IMAGE_JPEG)
                    .body(resource);
        } catch (Exception e) {
            // 捕获异常并打印到控制台
            System.out.println("[ERROR] 处理请求时发生异常，用户名：" + uName);
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(null); // 500 内部服务器错误
        }
    }




}
